package com.bdqn.t296.noso.config.shiro;

import com.bdqn.t296.noso.entity.Menu;
import com.bdqn.t296.noso.mapper.MenuMapper;
import com.bdqn.t296.noso.service.MenuService;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Resource
    MenuService menuService;

    @Bean
    public MyShiroRealm myShiroRealm() { //自定义 Realm
        MyShiroRealm shiroRealm = new MyShiroRealm();
        return shiroRealm;
    }

    @Bean
    public SecurityManager securityManager(@Qualifier("myShiroRealm") MyShiroRealm myShiroRealm) { //安全管理器 SecurityManager
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //注入 Realm
        securityManager.setRealm(myShiroRealm);
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactory(SecurityManager securityManager) {//Shiro 过滤器：权限验证
        ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean();
        //设置安全管理器注入 SecurityManager
        shiroFilterFactory.setSecurityManager(securityManager);
        //权限验证：使用 Filter 控制资源(URL)的访问
        shiroFilterFactory.setLoginUrl("/index");
        shiroFilterFactory.setSuccessUrl("/main");
        shiroFilterFactory.setUnauthorizedUrl("/403");

        /*
        anon：无需认证就能访问
        authc：必须认证了才能访问
        user：必须拥有 记住我 功能才能使用
        perms：拥有对某个资源的访问权限才能访问
        role：拥有某个角色权限才能访问
         */
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();//必须使用 LinkedHashMap(有序集合)
        //配置可以匿名访问的资源(URL)：静态资源
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/fonts/**", "anon");
        filterChainDefinitionMap.put("/images/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        filterChainDefinitionMap.put("/localcss/**", "anon");
        filterChainDefinitionMap.put("/localjs/**", "anon");

        filterChainDefinitionMap.put("/dologin", "anon");
        filterChainDefinitionMap.put("/logout", "logout");//注销过滤器，自动注销

        //配置权限验证：特定资源(URL)必须具备指定的权限才能访问
        //静态演示
        //*filterChainDefinitionMap.put("/user/list","perms[用户列表]");
        //filterChainDefinitionMap.put("/user/add","perms[用户添加]");
        //filterChainDefinitionMap.put("/user/edit","perms[用户编辑]");
        //filterChainDefinitionMap.put("/user/del","perms[用户删除]");*/
        //……其他省略
        //动态授权
        List<Menu> menus = menuService.selectAll();
        for (Menu menu : menus) {
            if (menu.getHref() != null && !menu.getHref().trim().equals("")) {
                filterChainDefinitionMap.put(menu.getHref(), "perms[" + menu.getId() + "]");
                System.out.println("授权地址" + menu.getHref());
            }
        }

        //配置认证访问：其他资源(URL)必须认证通过才能访问
        filterChainDefinitionMap.put("/**", "authc"); //必须放在过滤器链的最后面

        shiroFilterFactory.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactory;

    }
}
